// hyb_yl/zhuanjiasubpages/pages/suifangjilu/suifangjilu.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    year: 0,
    month: 0,
    date: ['日', '一', '二', '三', '四', '五', '六'],
    dateArr: [],
    isToday: 0,
    isTodayWeek: false,
    todayIndex: 0,
    // 周历
    selectWeek: 0,
    timeBean: {},
    list:[
      {
        time:'4月9日',
        sfcontent:{
          title:'随访内容',
          desc:'内容内容单卡双家啊卡的回复内容内容单卡双家啊卡的回复内容内容单卡双家啊卡的回复'
        },
        sfresult:{
          title:'随访结果',
          desc:'内容内容单卡双家啊卡的回复'
        },
        kftime:'2021-04-09 15:30',
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var zid = options.zid
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    that.dateInit();
    var j_id = options.j_id;
    if(zid != undefined)
    {
      that.setData({
        zid:zid
      })
    }
    that.setData({
      year: year,
      month: month,
      day: now.getDate(),
      timeBean: that.getWeekDayList(that.data.selectWeek),
      today: '' + year + '-' + that.timeFormat(month) + '-' + that.timeFormat(now.getDate()),
      isToday: '' + year + '-' + that.timeFormat(month) + '-' + that.timeFormat(now.getDate()),
      j_id:j_id
    })
    console.log("isToday", that.data.isToday)
    that.getSuifang();

  },
  getSuifang:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/hzbingli.suifang",
      data:{
        j_id:that.data.j_id,
        zid:that.data.zid,
        time:that.data.time,
        page:that.data.page
      },
      success:function(res){
        var page = that.data.page + 1
        that.setData({
          page:page,
          list:that.data.list.concat(res.data)
        })
      }
    })
  },
  //小于10的格式化函数（2变成02）
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  /**
   * 挑选日期---周 选中了某一日，改变selectDay为选中日
   */
  itemClick: function (e) {
    console.log("选择日期筛选---周", e)
    var timeBean = this.data.timeBean
    timeBean.selectDay = e.currentTarget.dataset.index;
    var that = this
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.cflistlog',
      data: {
        zid: that.data.zid,
        today: e.currentTarget.dataset.date
      },
      success: function (res) {
        // that.setData({
        //   list: res.data
        // })
      }
    })
    this.setData({
      timeBean,
      isToday: e.currentTarget.dataset.date
    })
  },
  row_btn(e) {
    this.setData({
      moth_show: !this.data.moth_show,
    })
  },
  // 挑选日期---月
  dayClick(e) {
    console.log("选择日期筛选---月", e)
    var that = this

    app.util.request({
      'url': 'entry/wxapp/zhuanjia.cflistlog',
      data: {
        zid: that.data.zid,
        today: e.currentTarget.dataset.date
      },
      success: function (res) {
        console.log(res)
        // that.setData({
        //   list: res.data
        // })
      }
    })
    that.setData({
      isToday: e.currentTarget.dataset.date
    })
  },
  // 月历
  dateInit: function (setYear, setMonth) {
    var that = this
    //全部时间的月份都是按0~11基准，显示月份才+1
    let dateArr = []; //需要遍历的日历数组数据
    let arrLen = 0; //dateArr的数组长度
    let now = setYear ? new Date(setYear, setMonth) : new Date();
    let year = setYear || now.getFullYear();
    let nextYear = 0;
    let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
    let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
    // let startWeek = new Date(year + ',' + (month + 1) + ',' + 1).getDay(); //目标月1号对应的星期
    let startWeek="";
    wx.getSystemInfo({
      success: function(res){
        if(res.platform=='ios'){
          startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目标月1号对应的星期
        }else{
          startWeek = new Date(year + '-' + (month + 1) + '-' + 1).getDay(); //目标月1号对应的星期
        }
      }
    })
    let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
    let obj = {};
    let num = 0;
    if (month + 1 > 11) {
      nextYear = year + 1;
      dayNums = new Date(nextYear, nextMonth, 0).getDate();
    }
    arrLen = startWeek + dayNums;
    for (let i = 0; i < arrLen; i++) {
      if (i >= startWeek) {
        num = i - startWeek + 1;
        obj = {
          isToday: '' + year + '-' +that.timeFormat(month + 1) +'-'+ that.timeFormat(num),
          dateNum: num,
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
    }
    that.setData({
      dateArr: dateArr
    })
    let nowDate = new Date();
    let nowYear = nowDate.getFullYear();
    let nowMonth = nowDate.getMonth() + 1;
    let nowWeek = nowDate.getDay();
    let getYear = setYear || nowYear;
    let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
    if (nowYear == getYear && nowMonth == getMonth) {
      that.setData({
        isTodayWeek: true,
        todayIndex: nowWeek
      })
    } else {
      that.setData({
        isTodayWeek: false,
        todayIndex: -1
      })
    }
    console.log("dateArr",that.data.dateArr)
  },
  // 上月
  lastMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
    let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  //下月 
  nextMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
    let month = this.data.month > 11 ? 0 : this.data.month;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  // 周历
  /**
   * 获取该周的所要显示的周和日期的对应数据，数据结构如下
   * var weekDay = {week: '',day: ''}
   * 参数：selectWeek  0为本周，数字代表前几周或者后几周，例如1是下一周
   */
  getWeekDayList(selectWeek) {
    // 1.获取周一对应得时间
    // 2.用循环七次添加周一到周日对应得周几和几号
    var that = this
    var selectWeekTime = that.getCurrentTimeStamp() + (selectWeek * 7) * 24 * 60 * 60 * 1000
    var mondayTime = selectWeekTime - (that.getWeekNumber(selectWeekTime) - 1) * 24 * 60 * 60 * 1000
    var timeBean = {
      selectDay: 0,
      yearMonth: '',
      weekDayList: []
    }
    for (var i = 0; i < 7; i++) {
      var weekDay = {
        week: '',
        day: ''
      }
      weekDay.week = that.getWeek(mondayTime + i * 24 * 60 * 60 * 1000)
      weekDay.day = that.getMyDay(mondayTime + i * 24 * 60 * 60 * 1000)
      weekDay.date = that.getYearMonth(selectWeekTime) + '-' + that.timeFormat(that.getMyDay(mondayTime + i * 24 * 60 * 60 * 1000))
      timeBean.weekDayList.push(weekDay)
    }
    for (var j = 0; j < timeBean.weekDayList.length; j++) {
      if (timeBean.weekDayList[j].day < timeBean.weekDayList[j + 1].day) {
        timeBean.weekDayList[j].day = that.getMyDay(mondayTime + j * 24 * 60 * 60 * 1000)
        return timeBean
      } else {
        var i = 1
        for (var i = 1; i < 7 - j; i++) {
          timeBean.weekDayList[j + i].day = ''
        }
        return timeBean
      }
    }
    timeBean.yearMonth = that.getYearMonth(selectWeekTime);
    // timeBean.todaydate = this.getYearMonth(selectWeekTime)+'-'+this.getMyDay(mondayTime + timeBean.selectDay * 24 * 60 * 60 * 1000)
    timeBean.selectDay = that.getCurrenrWeek();
    return timeBean;
  },


  //获取当前时间戳  --
  getCurrentTimeStamp() {
    var timestamp = new Date().getTime();
    return timestamp
  },

  //获取当前周几
  getCurrenrWeek() {
    var str = "6012345".charAt(new Date().getDay());
    console.log("获取当前周几str", str)
    return str;
  },

  //时间戳获得年月
  getYearMonth(res) {
    var time = new Date(res);
    var y = time.getFullYear();
    var m = this.timeFormat(time.getMonth() + 1);
    return y + "-" + m;
  },

  //时间戳转几号
  getMyDay(res) {
    var time = new Date(res);
    var d = time.getDate();
    return d;
  },

  //时间戳转周几 
  getWeek(res) {
    var time = new Date(res);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    // return "日一二三四五六".charAt(new Date(y + '-' + m + '-' + d).getDay());
    var g_week = "";
    wx.getSystemInfo({
      success: function(res){
        if(res.platform=='ios'){
          g_week = y + '/' + m + '/' + d
        }else{
          g_week = y + '-' + m + '-' + d
        }
      }
    })
    return "日一二三四五六".charAt(new Date(g_week).getDay());
  },

  //时间戳转周几 0123456  --
  getWeekNumber(res) {
    var time = new Date(res);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    // return "0123456".charAt(new Date(y + '-' + m + '-' + d).getDay());
    var n_week = "";
    wx.getSystemInfo({
      success: function(res){
        if(res.platform=='ios'){
          n_week = y + '/' + m + '/' + d
        }else{
          n_week = y + '-' + m + '-' + d
        }
      }
    })
    return "0123456".charAt(new Date(n_week).getDay());
  },
  /** 
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.cflistlog',
      data: {
        zid: that.data.zid,
        today: that.data.today
      },
      success: function (res) {
        console.log(res)
        // that.setData({
        //   list: res.data
        // })
      }
    })
  },
})